// 全局请求封装
// const base_url = 'https://www.dbdgtech.com/uatapi'
// const base_url = 'https://h5.dbdgtech.com/api01test/v1'
const base_url = 'https://www.dbdgtech.com/api'
// 请求超出时间
const timeout = 5000
import {showToast} from 'vant'
// 需要修改token，和根据实际修改请求头
export default (params) => {
	const token = uni.getStorageSync("Token")
	let url = params.url;
	let method = params.method || "get";
	let data = params.data || {};
	let header = {
		// 'Blade-Auth': uni.getStorageSync('token') || '',
		'Content-Type': 'application/json;charset=UTF-8',
		'Authorization': `${token}`,
    "X-Target":"accessPartyFrom=DB&userAccounts=xxx&tokenKey=xxx",
		// 'Tenant-Id': uni.getStorageSync('tenantId') || 'xxx', // avue配置相关
		...params.header
	}
	if (method == "post") {
		header = {
			'Content-Type': 'application/json',
      'Authorization': `${token}`,
      "X-Target":"accessPartyFrom=DB&userAccounts=xxx&tokenKey=xxx",
      ...params.header
		};
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: base_url + url,
			method: method,
			header: header,
			data: data,
			timeout,
			success(response) {
				const res = response
				// 根据返回的状态码做出对应的操作
				//获取成功
				// console.log(res.statusCode);
				if (res.data.code !== 401) {
					resolve(res.data);
				} else if(res.data.code===401){
          // uni.showToast({
          //   title:"登录已过期",
          //   icon:"none"
          // })
          showToast("请重新登录")
					uni.clearStorageSync()
          // console.log("登录")
          uni.reLaunch({
            url:"/pages/login/login"
          })

				}
			},
			fail(err) {
				console.log(err)
				if (err.errMsg.indexOf('request:fail') !== -1) {
					uni.showToast({
						title: '网络异常',
						icon: "error",
						duration: 2000
					})
				} else {
					uni.showToast({
						title: '未知异常',
						duration: 2000
					})
				}
				reject(err);

			},
			complete() {
				// 不管成功还是失败都会执行
				uni.hideLoading();
				uni.hideToast();
			}
		});
	}).catch(() => {});
};
